package com.litorg.demo.db.config;

import com.litorg.demo.db.utils.SpelUtil;
import com.litorg.demo.db.vo.BusinessData;
import java.lang.reflect.Method;
import java.util.Objects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Aspect
@Component
public class SendRedisMsgAspect {

    private static final Logger log = LoggerFactory.getLogger(SendRedisMsgAspect.class);

    @AfterReturning("@annotation(com.litorg.demo.db.config.SendRedisMsg)")
    public void afterReturning(JoinPoint point) throws Throwable {

        Signature signature = point.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        SendRedisMsg annotation = method.getAnnotation(SendRedisMsg.class);
        if (annotation == null) {
            return;
        }
        String subtype = null;
        if (StringUtils.hasText(annotation.subtype())) {
            if (annotation.subtype().contains("#")) {
                subtype = SpelUtil.excute(annotation.subtype(), point);
            } else {
                subtype = annotation.subtype();
            }
        }
        log.info(subtype);
        String stationId = null;
        if (StringUtils.hasText(annotation.stationId())) {
            if (annotation.stationId().contains("#")) {
                stationId = SpelUtil.excute(annotation.stationId(), point);
            } else {
                stationId = annotation.stationId();
            }
        }
        log.info(stationId);

        Object[] args = point.getArgs();
        if (!Objects.isNull(args) && args.length > 0 && (args[0] instanceof BusinessData)) {
            // System.out.println();
        }
        String topic = annotation.topic();
        log.info("topic: {}", topic);
    }
}